import request from "@/api/request";
import useStorage from "../../plugin/useStorage";
import {getImage} from "@/config/rangeImage";

const userinfo = useStorage.getItem('userinfo')
export const createProject = (form: { name: string; description: string; leader: string; startDate: string; endDate: string; cover: any; })=>{
    return request.post('/project',form)
}

export const  getProjectList = ()=>{
     //模拟15条项目数据
     const data:ProjectItemType[] = []
     for(let i=0;i<15;i++){
        data.push({
             id:i,
             name:'项目'+i,
             description:'项目'+i,
             leader:{id:i,username:'用户'+i,avatar:getImage()},
             startDate:'2021-01-01',
             endDate:'2021-01-01',
             cover: getImage(),
             progress:Math.ceil(Math.random()*100), //取整
             status:Math.random()*100>80?'进行中':'已完成',
             createdAt:formatDate(new Date()), 
             updatedAt:new Date().toISOString()
         })
     }
     return new Promise((resolve)=>{
         resolve(data)
     })
}
//时间格式YYYY/MM/DD
export const formatDate = (date:Date)=>{
    const year = date.getFullYear()
    const month = date.getMonth()+1
    const day = date.getDate()
    return `${year}-${month}-${day}`
}

export const getProjectStatus = ()=>{
    return request.get('/project/status',{user_id:userinfo.id})
}
export const CreateProjectStatus = (data:any)=>{
    data.user_id = userinfo.id

    return request.post('/project/status',data)
}
export const updateProjectStatus = (id:number,data:any)=>{
    return request.put('/project/status/'+id,data)
}
export const deleteProjectStatus = (id:number)=>{
    return request.delete('/project/status/'+id)
}